package com.nanjia.boot.manufacturer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.manufacturer.model.form.ManufacturerForm;
import com.nanjia.boot.manufacturer.model.query.ManufacturerQuery;
import com.nanjia.boot.manufacturer.model.vo.ManufacturerVO;
import com.nanjia.boot.manufacturer.service.ManufacturerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 医疗器械生产商信息前端控制层
 *
 * @author 周洋
 * @since 2025-08-19 13:42
 */
@Tag(name = "医疗器械生产商信息接口")
@RestController
@RequestMapping("/api/v1/manufacturer")
@RequiredArgsConstructor
public class ManufacturerController  {

    private final ManufacturerService manufacturerService;

    @Operation(summary = "医疗器械生产商信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('manufacturer:manufacturer:query')")
    public PageResult<ManufacturerVO> getManufacturerPage(ManufacturerQuery queryParams ) {
        IPage<ManufacturerVO> result = manufacturerService.getManufacturerPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增医疗器械生产商信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('manufacturer:manufacturer:add')")
    public Result<Void> saveManufacturer(@RequestBody @Valid ManufacturerForm formData, HttpServletRequest request ) {
        boolean result = manufacturerService.saveManufacturer(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取医疗器械生产商信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('manufacturer:manufacturer:edit')")
    public Result<ManufacturerForm> getManufacturerForm(
        @Parameter(description = "医疗器械生产商信息ID") @PathVariable Long id
    ) {
        ManufacturerForm formData = manufacturerService.getManufacturerFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改医疗器械生产商信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('manufacturer:manufacturer:edit')")
    public Result<Void> updateManufacturer(
            @Parameter(description = "医疗器械生产商信息ID") @PathVariable Long id,
            @RequestBody @Validated ManufacturerForm formData
    ) {
        boolean result = manufacturerService.updateManufacturer(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除医疗器械生产商信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('manufacturer:manufacturer:delete')")
    public Result<Void> deleteManufacturers(
        @Parameter(description = "医疗器械生产商信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = manufacturerService.deleteManufacturers(ids);
        return Result.judge(result);
    }
}
